package com.ssm.springboot.ExceptionHander;

import com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException;
import com.ssm.springboot.pojo.ErrorInfo;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * 统一异常处理类
 *
 * @author
 * @create 2018-07-10 11:04
 **/
@ControllerAdvice
public class ExceptionHander {

    public  static final String DEFAULT_ERROR_VIEW="error";
    public  static final String MYSQL_ERROR_VIEW="sql_error";
    /*@ExceptionHandler(value = Exception.class)
    public ModelAndView defaultErrorHandler(HttpServletRequest request ,Exception e){
        ModelAndView view = new ModelAndView();
        view.addObject("exception",e);
        view.addObject("url",request.getRequestURL());
        System.out.println(e);
        System.out.println(request.getRequestURL());
        view.setViewName(DEFAULT_ERROR_VIEW);
        return  view;
    }*/
    @ExceptionHandler(value = MySQLSyntaxErrorException.class)
    public ModelAndView mysqltErrorHandler(HttpServletRequest request ,Exception e){
        ModelAndView view = new ModelAndView();
        view.addObject("exception",e);
        view.addObject("url",request.getRequestURL());
        System.out.println(e);
        System.out.println(request.getRequestURL());
        view.setViewName(MYSQL_ERROR_VIEW);
        return  view;
    }

}
